$.error_pic = '<img class="alert-image" src="media/img/cross.png" border="0" />';
$.ok_pic = '<img class="alert-image" src="media/img/tick.png" border="0" />';
$(document).ready(function(){
	$('input[name=imie]').focus();
	$('input[name=imie]').keyup(function(){
		checkField('imie');
	});
	$('input[name=nazwisko]').keyup(function(){
		checkField('nazwisko');
	});
	$('input[name=email]').blur(function(){
		checkField('email');
	});
	$('input[name=password]').keyup(function(){
		$(this).parent().parent().find('.error').html(passwordStrength($(this).val()))
	});
	$('input[name=password_confirm]').keyup(function(){
		checkField('password_confirm');
	});
	$('#valid').keyup(function(){
		checkCaptcha();
	});
	
	$('#registerform').submit(function(){
		$('#flash-panel').html('');
		checkField('imie');
		checkField('nazwisko');
		checkField('email');
		checkField('password');
		checkField('password_confirm');
		checkCaptcha();
		if(checkField('imie') == false || checkField('nazwisko') == false || checkField('email') == false || checkField('password') == false || checkField('password_confirm') == false || checkCaptcha() == false){
			$('#submit-error').html('<span class="flash-error">Wprowadź poprawne dane</span>');
			return false;
		}else{
			$('#submit-error').html('');
			$("#submit-sending").html('<div class="flash-ok"><img src="media/img/loading_16.gif" border="0" /> Trwa wysyłanie...</div>');
		}
	});
})
function checkCaptcha(){
	var valid = $('#valid').val();
	//alert(valid);return false;
	var hid = $('#captcha_hid').val();
	if(valid != hid){
		$('.captcha-error').html($.error_pic+' Wprowadź poprawny wynik');
		return false;
	}else{
		$('.captcha-error').html($.ok_pic);
	}
}
function checkField(field)
{
	var edit = $('#check_edit').text();
	var field_name = $('input[name='+field+']');
	$.check_mail_avail = 0;
	if(field_name.val() == ''){
		if(edit != 'false'){
			if(field == 'password' || field == 'password_confirm'){
				var password = $('input[name=password]').val();
				var password_confirm = $('input[name=password_confirm]').val();
				if(password != password_confirm){
					field_name.parent().parent().find('.error').html($.error_pic+' Hasło się nie zgadza');
					return false;
				}
			}else{
				field_name.parent().parent().find('.error').html($.error_pic+' Pole nie może być puste');
				return false;
			}
		}else{
			field_name.parent().parent().find('.error').html($.error_pic+' Pole nie może być puste');
			return false;
		}
	}else{
		if(field == 'imie' && $('input[name=imie]').val() != ''){
			var val = $('input[name=imie]').val();
			var length = val.length;
			if(length < 3){
				field_name.parent().parent().find('.error').html($.error_pic+' Minimalna ilość znaków: 3');
				return false;
			}
		}
		if(field == 'nazwisko' && $('input[name=nazwisko]').val() != ''){
			var val = $('input[name=nazwisko]').val();
			var length = val.length;
			if(length < 3){
				field_name.parent().parent().find('.error').html($.error_pic+' Minimalna ilość znaków: 3');
				return false;
			}
		}
		if(field == 'email'){
			if(checkEmail(field_name.val()) == 0){
				field_name.parent().parent().find('.error').html($.error_pic+' Wprowadź poprawny e-mail');
				return false;
			}
			// sprawdzenie dostępności maila
			$.ajax({
				type: 'POST',
				async: false,
				beforeSend: function(){
					var loading = '<img src="media/img/loading_16.gif" border="0" />';
					field_name.parent().parent().find('.error').html(loading);
				},
				url: 'index.php/admin/checkmail',
				data: 'email='+field_name.val()+'&edit='+edit,
				error: function(xhr, desc, e) {
				   alert(xhr.responseText);
				},
				success: function(msg){
					//alert(msg);return false;
					if(msg == 1){
						field_name.parent().parent().find('.error').html($.error_pic+' Ten email został już użyty');
						$.check_mail_avail = 1;
					}
				}
			});
		}
		if(field == 'password_confirm'){
			var password = $('input[name=password]').val();
			var password_confirm = $('input[name=password_confirm]').val();
			if(password != password_confirm){
				field_name.parent().parent().find('.error').html($.error_pic+' Hasło się nie zgadza');
				return false;
			}
		}
		if($.check_mail_avail == 0){
				if(field != 'password')
					field_name.parent().parent().find('.error').html($.ok_pic);
			}else{
				return false;
			}
	}
}
function checkEmail(email) {
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if(email.search(emailRegEx) == -1) {
		return 0;
    }
}
// SIŁA HASŁA
var shortPass = '<img class="alert-image" src="media/img/emoticon_unhappy.png" border="0" /> Minimum 4 znaki'
var badPass = '<img class="alert-image" src="media/img/emoticon_surprised.png" border="0" /> <span style="color:black;">Hasło słabe</span>'
var goodPass = '<img class="alert-image" src="media/img/emoticon_smile.png" border="0" /> <span style="color:orange;">Hasło dobre</span>'
var strongPass = '<img class="alert-image" src="media/img/emoticon_happy.png" border="0" /> <span style="color:green;">Hasło mocne</span>'

function passwordStrength(password)
{
    score = 0 
    
    //password < 4
    if (password.length < 4 ) { return shortPass }
  
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 
    
    if (score < 34 )  return badPass 
    if (score < 68 )  return goodPass
    return strongPass
}
function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res;
}